package wrap

import (
	"excel-handle/ent/json"
	yml "excel-handle/ent/yaml"
	"excel-handle/util"
	"fmt"
	"gopkg.in/yaml.v3"
	"io"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"testing"
)

var rootDir string = ".\\"

// var rootDir string = "..\\"
var year int = 2024

var HolidayPath string = rootDir + "res\\holiday-" + fmt.Sprintf("%d", year) + ".json"

var DaysPath string = rootDir + "res\\days-" + fmt.Sprintf("%d", year) + ".json"

var FullDaysPath string = rootDir + "res\\full-days-" + fmt.Sprintf("%d", year) + ".json"

func WritePropFile(path string, prop yml.Prop) error {

	bytes, err := yaml.Marshal(prop)
	if err != nil {
		fmt.Println("yaml序列化失败")
		panic(err)
	}

	if err = os.WriteFile(path, bytes, os.ModePerm); err != nil {
		fmt.Println("文件写入失败")
		panic(err)
	}
	return err
}

func TestJsonReadHoliday(t *testing.T) {

	bufReader := util.ToBufReader(HolidayPath)
	data, _ := io.ReadAll(bufReader)
	//jsonObj := util.BytesToJson(data)
	var holiday json.Holiday
	// 传递的是引用，返回的还是该对象
	util.JsonBytesToStruct(data, &holiday)

	jsonStr := util.ToJsonString(holiday)
	fmt.Println(jsonStr)

}

func TestProgramPath(t *testing.T) {

	// 获取路径的方法

	wd, _ := os.Getwd()
	// 输出目录，看看路径对不对
	fmt.Println("工作目录:" + wd)

	println(os.Executable())
	filePath, _ := exec.LookPath(os.Args[0])
	fmt.Println("当前正在执行的文件:" + filePath)
	absFilePath, _ := filepath.Abs(filePath)
	rootDir := path.Dir(absFilePath)
	// 输出目录，看看路径对不对
	fmt.Println("程序根目录:" + rootDir)

	// 获取当前文件的路径
	_, filename, _, _ := runtime.Caller(0)
	fmt.Println("当前正在执行的代码的文件路径:" + filename)

}

func TestPath2(t *testing.T) {
	println(filepath.Dir(""))
	println(filepath.Base(""))
	println(filepath.Abs(""))
	println(filepath.Ext(""))
	println(filepath.Clean(""))

	println("========")

	//当前工作路径
	fmt.Println(os.Getwd())

	println(runtime.Caller(0))
	println(runtime.Caller(1))

}

func TestJsonReadDays(t *testing.T) {

	bufReader := util.ToBufReader(DaysPath)
	data, _ := io.ReadAll(bufReader)
	//jsonObj := util.BytesToJson(data)

	var days []json.DayInfo
	util.JsonBytesToStruct(data, &days)

	// 转换JSON字符串打印
	jsonStr := util.ToJsonString(days)
	fmt.Println(jsonStr)
}
